﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;

using PlanningGIS.Util.Convertor;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;
namespace PlanningGIS.DataCheck.model
{
    /// <summary>
    /// 水系名称标示码不一致检查（通过水系名称，结合《浙江省水利工程名称代码》，检查其标示码是否正确）
    /// </summary>
    public class HydNameCodeCheck : Check
    {
        public int Check(string errType, IFeatureLayer pFeatLyr, IGeometry pGeometry, string whereClause,
            string fname1name, string fname2name, string fname3name, string hcode1name, string hcode2name, string hcode3name)
        {
            IFeatureClass pFeatCls = pFeatLyr.FeatureClass;

            int idxfname1 = pFeatCls.FindField(fname1name);
            if (idxfname1 == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到名称[" + fname1name + "]字段，检查失败！");
                return -1;
            }

            int idxhcode1 = pFeatCls.FindField(hcode1name);
            if (idxhcode1 == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到标示码[" + hcode1name + "]字段，检查失败！");
                return -1;
            }

            int idxfname2 = -1;
            if (fname2name != "")
            {
                idxfname2 = pFeatCls.FindField(fname2name);
                if (idxfname2 == -1)
                {
                   LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到名称2[" + fname2name + "]字段，检查失败！");
                    return -1;
                }
            }

            int idxhcode2 = -1;
            if (hcode2name != "")
            {
                idxhcode2 = pFeatCls.FindField(hcode2name);
                if (idxhcode2 == -1)
                {
                   LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到标示码2[" + hcode2name + "]字段，检查失败！");
                    return -1;
                }
            }

            int idxfname3 = -1;
            if (fname3name != "")
            {
                idxfname3 = pFeatCls.FindField(fname3name);
                if (idxfname3 == -1)
                {
                   LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到名称3[" + fname3name + "]字段，检查失败！");
                    return -1;
                }
            }

            int idxhcode3 = -1;
            if (hcode3name != "")
            {
                idxhcode3 = pFeatCls.FindField(hcode3name);
                if (idxhcode3 == -1)
                {
                   LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到标示码3[" + hcode3name + "]字段，检查失败！");
                    return -1;
                }
            }

            ISpatialFilter pSpatialFileter = new SpatialFilterClass();
            if (pGeometry != null) pSpatialFileter.Geometry = pGeometry;

            pSpatialFileter.WhereClause = whereClause;

            pSpatialFileter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            pSpatialFileter.GeometryField = pFeatCls.ShapeFieldName;

            showFakePro(true);
            LogHelper.WriteLog("正在计算需要检查的要素数目...");

            int max = FeatureClassHelper.GetFeatureCountFromLayer(pFeatLyr, pSpatialFileter);// pFeatCls.FeatureCount(pSpatialFileter);
            

            LogHelper.WriteLog("计算完毕。共有" + max + "个要素参与检查。");
            showFakePro(false);

            if (max == 0)
            {
                LogHelper.WriteLog("没有满足检查要求的要素存在。");
                return -1;
            }

            setProcessBarMax(max);

            IFeatureCursor pFeatCur;
            pFeatCur = pFeatLyr.Search(pSpatialFileter, false);

            IFeature pFeature;

            int count = 0;
            int errCount = 0;

            while ((pFeature = pFeatCur.NextFeature()) != null)
            {
                if (IsStop()) break;

                setProcessBarCurrent(count);
                count++;

                string fname1value = StringHelper.ConvertWRTNull(pFeature.get_Value(idxfname1));
                if (fname1value == "0" || fname1value == " ") fname1value = "";

                string hcode1value = StringHelper.ConvertWRTNull(pFeature.get_Value(idxhcode1));
                if (hcode1value == "0" || hcode1value == " ") hcode1value = "";

                if ((fname1value == "" ^ hcode1value == "")||hcode1value !=GetHcodeFromFname (fname1value ))
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "标示码与名称不对应");
                    errCount++;
                    continue;
                }

                if (idxfname2 != -1 && idxhcode2 != -1)
                {
                    string fname2value = StringHelper.ConvertWRTNull(pFeature.get_Value(idxfname2));
                    if (fname2value == "0" || fname2value == " ") fname2value = "";

                    string hcode2value = StringHelper.ConvertWRTNull(pFeature.get_Value(idxhcode2));
                    if (hcode2value == "0" || hcode2value == " ") hcode2value = "";

                    if ((fname2value == "" ^ hcode2value == "") || hcode2value != GetHcodeFromFname(fname2value))
                    {
                        RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "标示码2与名称2不对应");
                        errCount++;
                        continue;
                    }
                }

                if (idxfname3 != -1 && idxhcode3 != -1)
                {
                    string fname3value = StringHelper.ConvertWRTNull(pFeature.get_Value(idxfname3));
                    if (fname3value == "0" || fname3value == " ") fname3value = "";

                    string hcode3value = StringHelper.ConvertWRTNull(pFeature.get_Value(idxhcode3));
                    if (hcode3value == "0" || hcode3value == " ") hcode3value = "";

                    if ((fname3value == "" ^ hcode3value == "") || hcode3value != GetHcodeFromFname(fname3value))
                    {
                        RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "标示码3与名称3不对应");
                        errCount++;
                        continue;
                    }
                }
            }

            finishPro.Invoke();

            DataCheckUtility.ReleaseComObject(pFeature);
            DataCheckUtility.ReleaseComObject(pFeatCur);

            LogHelper.WriteLog("水系名称标示码不一致检查结束。共发现" + errCount + "处错误！\n");

            return errCount;
        }

        /// <summary>
        /// 通过水系名称，以及外部的资料得到标示码（没有《浙江省水利工程名称代码》，暂时空着）
        /// </summary>
        /// <param name="fname"></param>
        /// <returns></returns>
        private string GetHcodeFromFname(string fname)
        {
            if (fname == "") return "";
            return "";
        }
    }
}
